package com.cheerful.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cheerful.blog.dao.CommentDao;
import com.cheerful.blog.entity.Comment;
import com.cheerful.blog.service.CommentService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * (Comment)表服务实现类
 *
 * @author makejava
 * @since 2022-02-07 13:15:03
 */
@Service("commentService")
public class CommentServiceImpl extends ServiceImpl<CommentDao, Comment> implements CommentService {

    @Override
    public List<Comment> listForArticle(Long blogId) {
        List<Comment> comments = this.baseMapper.listForArticle(blogId);
        List<Comment> collect = comments.stream()
                .filter(comment -> comment.getParentId() == 0)
                .peek(comment -> comment.setChildList(getChild(comment, comments)))
                .collect(Collectors.toList());
        return collect;
    }

    private List<Comment> getChild(Comment root, List<Comment> comments) {
        List<Comment> collect = comments.stream().filter(item -> item.getParentId().equals(root.getId()))
                .peek(comment -> comment.setChildList(getChild(comment, comments)))
                .collect(Collectors.toList());
        return collect;
    }
}

